FEXP Solver  1.0.0.0
FEXPFileHandler.h
Go to the documentation of this file.
1 // © FEXP, FEXPEnterprise Solver, Ing. Vaclav Rek
3 // Library for file handling.
4 // Compiler must support C++ ver.14 and later
6 #ifndef _CFEXPFILEHENDLER_H_
7 #define _CFEXPFILEHENDLER_H_
8 #include "FEXPCommon.h"
9 #include "FEXPSerializeData.h"
10 #include "FEXPConcurency.h"
11 
18 
21 {
22 public:
24  virtual Ptr<std::vector<std::string>> GetFileContent () const = 0;
25  virtual void Read () = 0;
26  virtual bool ReadProgress () = 0;
27 
28  // read file and get its content
29  static void ReadFileAndGetContent(const std::string & path, std::vector<std::string> & flines)
30  {
31 #define ERROR_RD "Error: File cannot read: "
32  std::ifstream file(path);
33  if (!file.is_open())
35  std::string line = FEXPCOMMON_EMPTY_STRING;
36  while (std::getline(file, line))
37  {
39  // check empty files or empty data
40  if (line == FEXPCOMMON_EMPTY_STRING)
41  continue;
42  // store line from file
43  flines.push_back(line);
44  }
45  // close file
46  file.close();
47  }
48 protected:
49  // [no protected members] -----------------------------
50 private:
51  // [no private members ] -----------------------------
52 };
53 
60 // primary template
61 template<typename TINContainer, typename Enable = void>
62 class CFEXPFileReader { };
65 template<typename TINContainer>
67  // specialization for ICFEXPInpDataContBase based types
68  <TINContainer, typename std::enable_if<std::is_base_of<ICFEXPInpDataContBase, TINContainer>::value>::type>
69  : public ICFEXPFileReaderIntf
70 {
71 public:
72  CFEXPFileReader(const std::string & path, const t_FileStructMap & map);
73  CFEXPFileReader(const std::string & path, Ptr<std::vector<std::string>> data, const t_FileStructMap & map);
74  virtual ~CFEXPFileReader();
75 
76  // override methods of interface
77  virtual Ptr<CFEXPFEInpContBase> GetInputContainer() override;
78  virtual Ptr<std::vector<std::string>> GetFileContent () const override;
79  virtual void Read () override;
80  virtual bool ReadProgress () override;
81 
82  Ptr<TINContainer> GetInputData();
83 protected:
84  // [no protected members] -----------------------------
85 private:
86  const std::string _inputpath;
88  // data container
89  bool _data_processed;
90  Ptr<TINContainer> _container;
91 };
92 
93 // member functions
95 //--------------------------------------------------------------------------------------
96 template<typename TINContainer>
98  ::CFEXPFileReader(const std::string & path, const t_FileStructMap & map)
99  : _inputpath(path), _content(CFEXPDataManager<std::vector<std::string>>::SafeAllocInstance()),
100  _data_processed(false), _container(CFEXPDataManager<TINContainer>::SafeAllocInstance(map)) { }
101 //--------------------------------------------------------------------------------------
102 
103 //--------------------------------------------------------------------------------------
104 template<typename TINContainer>
106  ::CFEXPFileReader(const std::string & path, Ptr<std::vector<std::string>> data, const t_FileStructMap & map)
107  : _inputpath(path), _content(data),
108  _data_processed(false), _container(CFEXPDataManager<TINContainer>::SafeAllocInstance(map)) { }
109 //--------------------------------------------------------------------------------------
110 
111 //--------------------------------------------------------------------------------------
112 template<typename TINContainer>
114  ::~CFEXPFileReader() { }
115 //--------------------------------------------------------------------------------------
116 
117 //--------------------------------------------------------------------------------------
118 template<typename TINContainer>
120  ::GetInputContainer()
121 //--------------------------------------------------------------------------------------
122 {
123  return FEXPCOMMON_DYNCAST(TINContainer, CFEXPFEInpContBase, _container);
124 }
125 
126 //--------------------------------------------------------------------------------------
127 template<typename TINContainer>
129  ::GetFileContent() const
130 //--------------------------------------------------------------------------------------
131 {
132  auto result = CFEXPDataManager<std::vector<std::string>>::SafeAllocInstance();
133  std::copy((*_content.get()).begin(), (*_content.get()).end(), std::back_inserter(*result.get()));
134  return result;
135 }
136 
139 //--------------------------------------------------------------------------------------
140 template<typename TINContainer>
142  ::Read()
143 //--------------------------------------------------------------------------------------
144 {
145  if (_data_processed)
146  return;
147  // read file and fill content vector
148  if(_content->empty())
149  ReadFileAndGetContent(_inputpath, *_content.get());
150  // create data
151  _container->ProcessLines(*_content.get());
152  // mark data as already processed
153  _data_processed = true;
154 }
155 
156 //--------------------------------------------------------------------------------------
157 template<typename TINContainer>
159  ::ReadProgress()
160 //--------------------------------------------------------------------------------------
161 {
162  return CFEXPAsyncRunner::RunProcedureTask([this] { Read(); }, true, "Reading of an input file", true);
163 }
164 
165 //--------------------------------------------------------------------------------------
166 template<typename TINContainer>
168  ::GetInputData()
169 //--------------------------------------------------------------------------------------
170 {
171  return _container;
172 }
173 
174 #endif // !_CFEXPFILEHENDLER_H_
virtual Ptr< CFEXPFEInpContBase > GetInputContainer()=0
Definition: FEXPFileHandler.h:62
static void ReadFileAndGetContent(const std::string &path, std::vector< std::string > &flines)
Definition: FEXPFileHandler.h:29
#define ERROR_RD
static std::string & StrTrim(std::string &str)
Definition: FEXPCommon.cpp:119
Definition: FEXPCommon.h:276
virtual Ptr< std::vector< std::string > > GetFileContent() const =0
virtual void Read()=0
virtual bool ReadProgress()=0
Base interface for file reader.
Definition: FEXPFileHandler.h:20
#define FEXPCOMMON_EXCEPTION(error_text)
Definition: FEXPCommon.h:143
#define FEXPCOMMON_EMPTY_STRING
Definition: FEXPCommon.h:183
std::map< std::string, std::function< Ptr< ICFEXPSerialDataBase >()> > t_FileStructMap
Definition: FEXPSerializeData.h:15
Base container interface.
Definition: FEXPSerializeData.h:601
static bool RunProcedureTask(t_Procedure procedure, bool progressbar)
It runs the asynchronous task.
Definition: FEXPConcurency.cpp:12
Smart pointer.
Definition: FEXPCommon.h:274
#define FEXPCOMMON_DYNCAST(clsfrom, clsto, variable)
Definition: FEXPCommon.h:138